pragma Singleton
import QtQuick 2.6

QtObject {
    function fix(val)
    {
        return fill(val, "0", 2, true)
    }
    function dateToReadable(date)
    {
        var target = new Date(date)
        var cur = new Date()
        if(cur.getDate() === target.getDate())
            return fix(target.getHours()) + ":" + fix(target.getMinutes()) + ":" + fix(target.getSeconds())
        return target.getMonth() + qsTr("月") + target.getDate()+ qsTr("日")

/*
        var diff = (cur - target) / 1000
        diff = diff.toFixed(0)
        if(diff < 3600)
            return String(diff / 60) + qsTr("分钟前");
        if(diff < 86400)
            return String(diff / 3600) + qsTr("小时前");
        if(diff < 2592000)
            return String(diff / 86400) + qsTr("天前");
        if(diff < 31104000)
            return String(diff / 2592000) + qsTr("月前");
        return String(diff / 31104000) + qsTr("年前");
        */
    }

    function msToReadable(ms)
    {
        var minute = Math.floor(ms / 60000)
        var second = Math.floor((ms % 60000) / 1000)
        minute = fill(minute, '0', 2, true)
        second = fill(second, '0', 2, true)
        return minute + ":" + second
    }

    // fill(123, "0", 6, true) -> "000123"
    // fill(123, "0", 6, false) -> "123000"
    function fill(str, val, length, leftFill)
    {
        var strstr = String(str)
        var strval = String(val)
        leftFill = leftFill || false
        while(strstr.length < length)
        {
            strstr = leftFill ? strval + strstr : strstr + strval
        }
        return strstr
    }

    function destroyAll(objlist)
    {
        for(var i=0; i < objlist.length; i++)
        {
            objlist[i].destroy()
        }
    }

    function printObject(obj, name)
    {
        print("------------- object printing start")
        name = name || "obj"
        for(var str in obj)
        {
            print(name + "." + str + " : " + obj[str])
        }
        print("------------- object printing end")
    }
}
